from backend.app.app.db.session_project import session_pro
import backend.app.app.models.db_owi_private_models as md_pro
from pydantic import BaseModel

#   复制其他页面信息
def copy_page(old_user_id:str,old_pro_id:str,new_pro_id:str):
    page1 = False
    page2 = False
    page3 = False
    try:
        old_data = session_pro.query(md_pro.TbUserprojPage1.Page_data,
                                        md_pro.TbUserprojPage1.Fliter_data,
                                        md_pro.TbUserprojPage1.Set_info,
                                        md_pro.TbUserprojPage1.Picture1,
                                        md_pro.TbUserprojPage1.Picture2,
                                        md_pro.TbUserprojPage1.Picture3,
                                        md_pro.TbUserprojPage1.Ramake)\
            .filter(md_pro.TbUserprojPage1.User_ID == old_user_id,
                    md_pro.TbUserprojPage1.Proj_ID == old_pro_id).all()
        new_data = md_pro.TbUserprojPage1(User_ID = old_user_id,
                                          Proj_ID = new_pro_id,
                                          Page_data = old_data[0][0],
                                          Fliter_data = old_data[0][1],
                                          Set_info = old_data[0][2],
                                          Picture1 = old_data[0][4],
                                          Picture2 = old_data[0][5],
                                          Picture3 = old_data[0][6],
                                          Ramake = old_data[0][6])
        session_pro.add(new_data)
        session_pro.commit()
        page1 = True
    except Exception as e:
        print('复制页面1失败', e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        page1 = False
    try:
        old_data = session_pro.query(md_pro.TbUserprojPage2.Page_data,
                                        md_pro.TbUserprojPage2.Fliter_data,
                                        md_pro.TbUserprojPage2.Set_info,
                                        md_pro.TbUserprojPage2.Picture1,
                                        md_pro.TbUserprojPage2.Picture2,
                                        md_pro.TbUserprojPage2.Picture3,
                                        md_pro.TbUserprojPage2.Ramake)\
            .filter(md_pro.TbUserprojPage2.User_ID == old_user_id,
                    md_pro.TbUserprojPage2.Proj_ID == old_pro_id).all()
        new_data = md_pro.TbUserprojPage2(User_ID = old_user_id,
                                          Proj_ID = new_pro_id,
                                          Page_data = old_data[0][0],
                                          Fliter_data = old_data[0][1],
                                          Set_info = old_data[0][2],
                                          Picture1 = old_data[0][4],
                                          Picture2 = old_data[0][5],
                                          Picture3 = old_data[0][6],
                                          Ramake = old_data[0][6])
        session_pro.add(new_data)
        session_pro.commit()
        page2 = True
    except Exception as e:
        print('复制页面2失败', e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        page2 = False
    try:
        old_data = session_pro.query(md_pro.TbUserprojPage3.Page_data,
                                        md_pro.TbUserprojPage3.Fliter_data,
                                        md_pro.TbUserprojPage3.Set_info,
                                        md_pro.TbUserprojPage3.Picture1,
                                        md_pro.TbUserprojPage3.Picture2,
                                        md_pro.TbUserprojPage3.Picture3,
                                        md_pro.TbUserprojPage3.Ramake)\
            .filter(md_pro.TbUserprojPage3.User_ID == old_user_id,
                    md_pro.TbUserprojPage3.Proj_ID == old_pro_id).all()
        new_data = md_pro.TbUserprojPage3(User_ID = old_user_id,
                                          Proj_ID = new_pro_id,
                                          Page_data = old_data[0][0],
                                          Fliter_data = old_data[0][1],
                                          Set_info = old_data[0][2],
                                          Picture1 = old_data[0][4],
                                          Picture2 = old_data[0][5],
                                          Picture3 = old_data[0][6],
                                          Ramake = old_data[0][6])
        session_pro.add(new_data)
        session_pro.commit()
        page3 = True
    except Exception as e:
        print('复制页面2失败', e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        page3 = False
    return page1,page2,page3


class u_p_id(BaseModel):
    user_id:str
    proj_id:str
    remark:str

def get_remark(user_id:str,pro_id:str):
    try:
        project_inf = session_pro.query(md_pro.TbUserprojBasic.Remark)\
            .filter(md_pro.TbUserprojBasic.User_ID == user_id,
                    md_pro.TbUserprojBasic.Proj_ID == pro_id).all()
        return project_inf
    except Exception as e:
        print('查找失败', e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

def revise_re(info:u_p_id):
    try:
        session_pro.query(md_pro.TbUserprojBasic).filter(md_pro.TbUserprojBasic.User_ID == info.user_id,
                                                         md_pro.TbUserprojBasic.Proj_ID == info.proj_id).update(
            {md_pro.TbUserprojBasic.Remark:info.remark})
        session_pro.commit()
        print('修改备注成功')
        return True
    except Exception as e:
        print('修改备注失败', e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

#存储新建的工程
def save_project_db(user_ID,proj_ID,proj_Name,proj_CreatDateTime,proj_UpdateTime,remark):
    new_project = md_pro.TbUserprojBasic(User_ID=user_ID,
                                         Proj_ID=proj_ID,
                                         Proj_Name=proj_Name,
                                         Proj_CreatDateTime=proj_CreatDateTime,
                                         Proj_UpdateTime=proj_UpdateTime,
                                         Remark=remark)
    try:
        session_pro.add(new_project)
        session_pro.commit()
        print('新建工程成功')
        session_pro.close()
        return True
    except Exception as e:
        print('新建工程失败', e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

#根据user_id获取所有的已有工程
def get_all_project(user_id):
    try:
        project_inf = session_pro.query(
            md_pro.TbUserprojBasic.User_ID,
            md_pro.TbUserprojBasic.Proj_ID,
            md_pro.TbUserprojBasic.Proj_Name,
            md_pro.TbUserprojBasic.Proj_CreatDateTime,
            md_pro.TbUserprojBasic.Proj_UpdateTime
        ).filter(
            md_pro.TbUserprojBasic.User_ID==user_id
        ).all()
        result = [dict(zip(data.keys(), data)) for data in project_inf]
        return result
    except Exception as e:
        print('查找工程失败', e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

#根据user_id和pro_id获取共享的工程信息
def get_share_pro(pro_id,user_id):
    try:
        share_pro = session_pro.query(md_pro.TbUserprojBasic.User_ID,
                                      md_pro.TbUserprojBasic.Proj_ID,
                                        md_pro.TbUserprojBasic.Proj_Name,
                                        md_pro.TbUserprojBasic.Proj_CreatDateTime,
                                        md_pro.TbUserprojBasic.Proj_UpdateTime)\
            .filter(md_pro.TbUserprojBasic.User_ID == user_id,
                    md_pro.TbUserprojBasic.Proj_ID == pro_id).all()
        session_pro.close()
        return share_pro
    except Exception as e:
        print(e, e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

